//
//  MYStatusPictureView.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/25.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let pictureMargin: CGFloat = 3
private let pictureCellId = "pictureCellId"
private let maxWidth = ScreenWidth - 2 * StatusCellMargin
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3

class MYStatusPictureView: UICollectionView {
    
    var pictures: [MYStatusPicture]? {
        didSet {
            
            //testLabel.text = "\(pictures?.count ?? 0)"
            //得到collectionView的视图大小
            let Psize = calculatePictureViewSize()
            
            //MARK:设置collectionView的大小,这里要用update,不然会造成cell重用的问题
            self.snp_updateConstraints { (make) in
                make.size.equalTo(Psize)
            }
            //每次给图片数组赋值,就说明要加载新的collectionView,要刷新数据源
            reloadData()
        }
    }
    
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        self.backgroundColor = UIColor.clearColor()
        
        self.scrollsToTop = false
        
       // setupUI()
        //注册cell
        registerClass(StatusPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        self.dataSource = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //计算collectionView的大小
    private func calculatePictureViewSize() -> CGSize {
        
        let pictureCount = pictures?.count ?? 0
        //0张
        if pictureCount == 0 {
            return CGSizeZero
        }
        
        //定义布局对象为流布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //1张
        if pictureCount == 1 {
            
            let imageSize = CGSizeMake(100, 150)
            layout.itemSize = imageSize
            return imageSize
        }
        
        //4张
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        if pictureCount == 4 {
            
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
        }
        
        //多张
        let row = CGFloat((pictureCount - 1) / 3 + 1)
        let h = row * itemWidth + (row - 1) * pictureMargin
        return CGSize(width: maxWidth, height: h)
        
    }
    
    
    //布局子控件
    private func setupUI() {
        addSubview(testLabel)
        
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    
    private lazy var testLabel: UILabel = UILabel(title: "", font: 33, textColor: UIColor.redColor())

}



///数据方法
extension MYStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! StatusPictureCell
        
        cell.picture = pictures![indexPath.item]
        
        return cell
    }
    
}




class StatusPictureCell: UICollectionViewCell {
    
    var picture: MYStatusPicture? {
        didSet {
            let imgUrlString = picture?.thumbnail_pic ?? ""
            
            //MARK:加载图片
            imageView.sd_setImageWithURL(NSURL(string: imgUrlString))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = UIColor.grayColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //布局imageView在cell中的布局
    private func setupUI() {
        
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    
    //懒加载imageView
    private lazy var imageView: UIImageView = {
        let img = UIImageView()
        //设置填充模式
        img.contentMode = .ScaleAspectFill
        //手写代码需要手动裁剪, xib/sb中会自动设置裁剪
        img.clipsToBounds = true
        return img
    }()

    
}
